package renguanyu.app.codebuilder.view.createcode;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.layout.BorderPane;
import renguanyu.app.codebuilder.model.Template;
import renguanyu.app.codebuilder.util.DaoFactory;
import renguanyu.app.codebuilder.view.CodeBuilderView;


public class CreateCodeView extends BorderPane{

    CodeBuilderView codeBuilderView;
    CreateCodeTopView createCodeTopView = new CreateCodeTopView(this);
    CreateCodeCenterView createCodeCenterView = new CreateCodeCenterView(this);
    CreateCodeBottomView createCodeBottomView = new CreateCodeBottomView(this);

    public CreateCodeView(CodeBuilderView codeBuilderView){
        this.codeBuilderView = codeBuilderView;
        initUI(); 
        initEvent();
        initData();
    }

    private void initUI() {
        this.setTop(createCodeTopView);
        this.setCenter(createCodeCenterView);
        this.setBottom(createCodeBottomView);

    }


    private void initEvent() {

    }

    public void initData() {

        ObservableList<Template> templateObList = FXCollections.observableArrayList();
        ObservableList<ObservableMap<String, String>> templateList = DaoFactory.getTemplateDao().query(null);
//        SingleSelectionModel<Template> selectionModel = createCodeTopView.getTemplateListCombo().getSelectionModel();
//        createCodeTopView.getTemplateListCombo().getItems().clear();


        CreateCodeTopView createCodeTopView = getCreateCodeTopView();
        SingleSelectionModel<Template> selectionModel = createCodeTopView.getTemplateListCombo().getSelectionModel();
//        System.out.println("------->");
//        System.out.println(selectionModel);
//        Template template1 = selectionModel.getSelectedItem();
//        System.out.println(template1);
//        createCodeTopView.getTemplateListCombo().setSelectionModel(selectionModel);
        createCodeTopView.getTemplateListCombo().getItems().clear();
        for(int i=0;i<templateList.size();i++){

            ObservableMap<String, String> map = templateList.get(i);
            String tempId = map.get("TEMP_ID");
            String tempName = map.get("TEMP_NAME");

            Template template = new Template();
            template.setId(tempId);
            template.setName(tempName);
            templateObList.add(template);
//            createCodeTopView.getTemplateListCombo().getItems().add(i,template);
        }
        createCodeTopView.getTemplateListCombo().getItems().setAll(templateObList);
//        System.out.println(createCodeTopView.getTemplateListCombo().getSelectionModel().getSelectedItem());
//        System.out.println("<-------------");
    }


    public CreateCodeTopView getCreateCodeTopView() {
        return createCodeTopView;
    }

    public CreateCodeCenterView getCreateCodeCenterView() {
        return createCodeCenterView;
    }

    public CreateCodeBottomView getCreateCodeBottomView() {
        return createCodeBottomView;
    }

    public CodeBuilderView getCodeBuilderView() {
        return codeBuilderView;
    }
}
